<template>
  <div>
    <el-card>
      <el-form inline>
        <el-form-item label="一级分类" prop="region" size="small" class="mt-right">
          <el-select
            placeholder="请选择"
            class="select"
            v-model="category1Id"
            @change="select1"
            :disabled="selectDisable"
          >
            <el-option
              v-for="item in category1List"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" prop="region" size="small" class="mt-right">
          <el-select
            placeholder="请选择"
            class="select"
            v-model="category2Id"
            @change="select2"
            :disabled="selectDisable"
          >
            <el-option
              v-for="item in category2List"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="三级分类" prop="region" size="small">
          <el-select
            placeholder="请选择"
            class="select"
            v-model="category3Id"
            @change="select3"
            :disabled="selectDisable"
          >
            <el-option
              v-for="item in category3List"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-show="!addOrEdit">
      <el-button
        icon="el-icon-plus"
        type="primary"
        size="small"
        class="mt-bottom"
        :disabled="!category3Id"
        @click="addOrEdit = 1"
      >
        添加SPU
      </el-button>
      <el-table :data="spuList" style="width: 100%" border class="mt-bottom">
        <el-table-column type="index" label="序号" width="60" align="center"> </el-table-column>
        <el-table-column prop="spuName" label="spu名称" width="350"> </el-table-column>
        <el-table-column prop="description" label="spu描述" width="350"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <template v-slot="{ row }">
            <el-button type="primary" size="mini" class="button" @click="addSkuFn(row)"
              ><i class="el-icon-plus icon"></i
            ></el-button>
            <el-button @click="editor(row)" type="primary" size="mini" class="button"
              ><i class="el-icon-edit-outline icon"></i
            ></el-button>
            <el-button type="info" size="mini" class="button"
              ><i class="el-icon-info icon"></i
            ></el-button>
            <el-button type="danger" size="mini" class="button" @click="deleteSpu(row)"
              ><i class="el-icon-delete icon"></i
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[3, 6, 9, 12]"
        :page-size="limit"
        layout="prev, pager, next, jumper,->,sizes,total"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <AddOrEditSpu v-if="addOrEdit == 1 || addOrEdit == 2" @toggle="toggle" :addOrEdit="addOrEdit" />
    <AddSku v-if="addOrEdit == 3" @toggle="toggle" />
  </div>
</template>

<script>
import { mapGetters, mapActions, mapState } from 'vuex'
import AddOrEditSpu from './addOrEditSpu/index.vue'
import AddSku from './addSku/index'
export default {
  name: 'Spu',
  data() {
    return {
      category1Id: null,
      category2Id: null,
      category3Id: null,
      buttonDisable: true,
      addOrEdit: 0,
      page: 1,
      limit: 3
    }
  },
  computed: {
    ...mapGetters(['category1List', 'category2List', 'category3List', 'attrInfoList']),
    ...mapState({
      spuList: state => {
        return state.spu.spuList.records
      },
      total: state => {
        return state.spu.spuList.total
      }
    }),
    selectDisable() {
      return Boolean(this.addOrEdit)
    }
  },
  components: {
    AddOrEditSpu,
    AddSku
  },
  async beforeMount() {
    await this.$store.dispatch('attr/getCategory1List')
  },
  methods: {
    ...mapActions('spu', ['getSpuList', 'AddSpu', 'updateSpu', 'reqDeleteSpu']),
    //  选择时触发的函数
    async select1(id) {
      this.$store.commit('attr/SETCATEGORY1ID', id)
      this.category2Id = null
      this.category3Id = null
      this.buttonDisable = true
      await this.$store.dispatch('attr/getCategory2List')
    },
    async select2(id) {
      this.$store.commit('attr/SETCATEGORY2ID', id)
      this.category3Id = null
      this.buttonDisable = true
      await this.$store.dispatch('attr/getCategory3List')
    },
    async select3(id) {
      this.$store.commit('attr/SETCATEGORY3ID', id)
      this.buttonDisable = false
      await this.$store.dispatch('attr/getAttrInfoList')
      this.requestSpuList()
    },
    toggle(number) {
      this.addOrEdit = number
      if (number == 0) {
        this.requestSpuList()
      }
    },
    editor(row) {
      console.log(row)
      this.$store.commit('spu/SETSPUID', row.id)
      this.addOrEdit = 2
    },
    requestSpuList() {
      const { page, limit, category3Id } = this
      this.getSpuList({ page, limit, category3Id })
    },
    handleSizeChange(val) {
      this.limit = val
      this.requestSpuList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.requestSpuList()
    },
    async deleteSpu(row) {
      // console.log(row)
      await this.reqDeleteSpu(row.id)
      this.$message.success('删除spu成功!!')
      this.requestSpuList()
    },
    addSkuFn(row) {
      this.$store.commit('spu/SETSPUID', row.id)
      // console.log(row)
      this.$store.commit('spu/SPUNAME', row.spuName)
      this.addOrEdit = 3
    }
  }
}
</script>

<style lang="scss" scoped>
.mt-right {
  margin-right: 50px;
}
</style>
